<template>
  <div>
    <div>
      <span>全选:</span>
      <input type="checkbox" v-model="flag" />
      <button @click="fn">反选</button>
    </div>
    <ul>
      <li v-for="(item, index) in arr" :key="index">
        <input type="checkbox" v-model="item.checked" />
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: [
        {
          name: "猪八戒",
          checked: false,
        },
        {
          name: "孙悟空",
          checked: false,
        },
        {
          name: "唐僧",
          checked: false,
        },
        {
          name: "白龙马",
          checked: false,
        },
      ],
    };
  },
  methods: {
    fn() {
      this.arr.forEach((item) => (item.checked = !item.checked));
    },
  },
  computed: {
    flag: {
      set(bool) {
        this.arr.forEach((item) => (item.checked = bool));
      },
      get() {
        return this.arr.every((obj) => obj.checked == true);
        // return this.arr.reduce((bool, obj) => {
        //   return bool && obj.checked;
        // }, true);
      },
    },
  },
};
</script>

<style>
</style>